﻿Imports System
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design

Namespace Controls
    ''' <summary> 
    ''' Popup window control 
    ''' </summary> 
    <DefaultProperty("Message"), ToolboxData("<{0}:PopupWin runat=server></{0}:PopupWin>")> _
    <Designer(GetType(PopupWinDesigner))> _
    Public Class PopupWin
        Inherits System.Web.UI.WebControls.WebControl
        Implements IPostBackEventHandler
#Region "Private variables, constants and constructor"

        Private msg As String, fullmsg As String, divDesign As String, cntStyle As String, sPopup As String, spopStyle As String, _
        cntStyleN As String, cntStyleI As String, aStyle As String, aCommands As String, hdrStyle As String, m_title As String, _
        closeHtml As String

        Private popupBackground As String, popupBorderDark As String, popupBorderLight As String, cntBorderDark As String, cntBorderLight As String, cntBackground As String, _
        gradientStart As String, gradientEnd As String, m_textColor As String, xButton As String, xButtonOver As String, sLink As String, _
        sTarget As String

        Private clrStyle As PopupColorStyle
        Private popDock As PopupDocking
        Private popAction As PopupAction

        Private xOffset As Integer, yOffset As Integer, iSpeed As Integer = 20
        Private winSize As Size
        Private winScroll As Boolean = True, bAutoShow As Boolean = True, bDragDrop As Boolean = False, bShowLink As Boolean = True

        Private iHide As Integer, startTime As Integer

        Const sScript As String = "<script type=""text/javascript"">" & Chr(13) & "" & Chr(10) & " //<![CDATA[" & Chr(13) & "" & Chr(10) & " var [id]oldonloadHndlr=window.onload, [id]popupHgt, [id]actualHgt, [id]tmrId=-1, [id]resetTimer;" & Chr(13) & "" & Chr(10) & " var [id]titHgt, [id]cntDelta, [id]tmrHide=-1, [id]hideAfter=[hide], [id]hideAlpha, [id]hasFilters=[ie];" & Chr(13) & "" & Chr(10) & " var [id]nWin, [id]showBy=null, [id]dxTimer=-1, [id]popupBottom, [id]oldLeft;" & Chr(13) & "" & Chr(10) & " var [id]nText,[id]nMsg,[id]nTitle,[id]bChangeTexts=false;" & Chr(13) & "" & Chr(10) & " window.onload=[id]espopup_winLoad;" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " var [id]oldonscrollHndr=window.onscroll;" & Chr(13) & "" & Chr(10) & " window.onscroll=[id]espopup_winScroll;" & Chr(13) & "" & Chr(10) & " [id]nText=""[popup]"";" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function [id]espopup_winScroll()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " if ([id]oldonscrollHndr!=null) [id]oldonscrollHndr();" & Chr(13) & "" & Chr(10) & " if ([id]tmrHide!=-1)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]');" & Chr(13) & "" & Chr(10) & " el.style.display='none'; el.style.display='block';" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function [id]espopup_ShowPopup(show)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " if ([id]dxTimer!=-1) { el.filters.blendTrans.stop(); }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " if (([id]tmrHide!=-1) && ((show!=null) && (show==[id]showBy)))" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " clearInterval([id]tmrHide);" & Chr(13) & "" & Chr(10) & " [id]tmrHide=setInterval([id]espopup_tmrHideTimer,[id]hideAfter);" & Chr(13) & "" & Chr(10) & " return;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " if ([id]tmrId!=-1) return;" & Chr(13) & "" & Chr(10) & " [id]showBy=show;" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " elCnt=document.getElementById('[id]_content')" & Chr(13) & "" & Chr(10) & " elTit=document.getElementById('[id]_header');" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]');" & Chr(13) & "" & Chr(10) & " el.style.left=[id]oldLeft;" & Chr(13) & "" & Chr(10) & " el.style.top='';" & Chr(13) & "" & Chr(10) & " el.style.filter='';" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " if ([id]tmrHide!=-1) clearInterval([id]tmrHide); [id]tmrHide=-1;" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_header').style.display='none';" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_content').style.display='none';" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " if (navigator.userAgent.indexOf('Opera')!=-1)" & Chr(13) & "" & Chr(10) & " el.style.bottom=(document.body.scrollHeight*1-document.body.scrollTop*1" & Chr(13) & "" & Chr(10) & " -document.body.offsetHeight*1+1*[id]popupBottom)+'px';" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " if ([id]bChangeTexts)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " [id]bChangeTexts=false;" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]aCnt').innerHTML=[id]nMsg;" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]titleEl').innerHTML=[id]nTitle;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " [id]actualHgt=0; el.style.height=[id]actualHgt+'px';" & Chr(13) & "" & Chr(10) & " el.style.visibility='';" & Chr(13) & "" & Chr(10) & " if (![id]resetTimer) el.style.display='';" & Chr(13) & "" & Chr(10) & " [id]tmrId=setInterval([id]espopup_tmrTimer,([id]resetTimer?[stime]:20));" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function [id]espopup_winLoad()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " if ([id]oldonloadHndlr!=null) [id]oldonloadHndlr();" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " elCnt=document.getElementById('[id]_content')" & Chr(13) & "" & Chr(10) & " elTit=document.getElementById('[id]_header');" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]');" & Chr(13) & "" & Chr(10) & "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "[id]oldLeft=el.style.left;" & Chr(13) & "" & Chr(10) & " [id]popupBottom=el.style.bottom.substr(0,el.style.bottom.length-2);" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " [id]titHgt=elTit.style.height.substr(0,elTit.style.height.length-2);" & Chr(13) & "" & Chr(10) & " [id]popupHgt=el.style.height;" & Chr(13) & "" & Chr(10) & " [id]popupHgt=[id]popupHgt.substr(0,[id]popupHgt.length-2); [id]actualHgt=0;" & Chr(13) & "" & Chr(10) & " [id]cntDelta=[id]popupHgt-(elCnt.style.height.substr(0,elCnt.style.height.length-2));" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " if ([autoshow])" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " [id]resetTimer=true;" & Chr(13) & "" & Chr(10) & " [id]espopup_ShowPopup(null);" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function [id]espopup_tmrTimer()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]');" & Chr(13) & "" & Chr(10) & " if ([id]resetTimer)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el.style.display='';" & Chr(13) & "" & Chr(10) & " clearInterval([id]tmrId); [id]resetTimer=false;" & Chr(13) & "" & Chr(10) & " [id]tmrId=setInterval([id]espopup_tmrTimer,[speed]);" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " [id]actualHgt+=5;" & Chr(13) & "" & Chr(10) & " if ([id]actualHgt>=[id]popupHgt)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " [id]actualHgt=[id]popupHgt; clearInterval([id]tmrId); [id]tmrId=-1;" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_content').style.display='';" & Chr(13) & "" & Chr(10) & " if ([id]hideAfter!=-1) [id]tmrHide=setInterval([id]espopup_tmrHideTimer,[id]hideAfter);" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " if ([id]titHgt<[id]actualHgt-6)" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_header').style.display='';" & Chr(13) & "" & Chr(10) & " if (([id]actualHgt-[id]cntDelta)>0)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " elCnt=document.getElementById('[id]_content')" & Chr(13) & "" & Chr(10) & " elCnt.style.display='';" & Chr(13) & "" & Chr(10) & " elCnt.style.height=([id]actualHgt-[id]cntDelta)+'px';" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " el.style.height=[id]actualHgt+'px';" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " function [id]espopup_tmrHideTimer()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " clearInterval([id]tmrHide); [id]tmrHide=-1;" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]');" & Chr(13) & "" & Chr(10) & " if ([id]hasFilters)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " backCnt=document.getElementById('[id]_content').innerHTML;" & Chr(13) & "" & Chr(10) & " backTit=document.getElementById('[id]_header').innerHTML;" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_content').innerHTML='';" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_header').innerHTML='';" & Chr(13) & "" & Chr(10) & " el.style.filter='blendTrans(duration=1)';" & Chr(13) & "" & Chr(10) & " el.filters.blendTrans.apply();" & Chr(13) & "" & Chr(10) & " el.style.visibility='hidden';" & Chr(13) & "" & Chr(10) & " el.filters.blendTrans.play();" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_content').innerHTML=backCnt;" & Chr(13) & "" & Chr(10) & " document.getElementById('[id]_header').innerHTML=backTit;" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " [id]dxTimer=setInterval([id]espopup_dxTimer,1000);" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " else el.style.visibility='hidden';" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " function [id]espopup_dxTimer()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " clearInterval([id]dxTimer); [id]dxTimer=-1;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " function [id]espopup_Close()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " if ([id]tmrId==-1)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]');" & Chr(13) & "" & Chr(10) & " el.style.filter='';" & Chr(13) & "" & Chr(10) & " el.style.display='none';" & Chr(13) & "" & Chr(10) & " if ([id]tmrHide!=-1) clearInterval([id]tmrHide); [id]tmrHide=-1;" & Chr(13) & "" & Chr(10) & " [sclose]" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " " & Chr(13) & "" & Chr(10) & " function [id]espopup_ShowWindow()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " [slink]" & Chr(13) & "" & Chr(10) & " if ([id]nWin!=null) [id]nWin.close();" & Chr(13) & "" & Chr(10) & " [id]nWin=window.open('','[id]nWin','[winstyle], '+" & Chr(13) & "" & Chr(10) & " 'menubar=no, resizable=no, status=no, toolbar=no, location=no');" & Chr(13) & "" & Chr(10) & " [id]nWin.document.write([id]nText);" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " var [id]mousemoveBack,[id]mouseupBack;" & Chr(13) & "" & Chr(10) & " var [id]ofsX,[id]ofsY;" & Chr(13) & "" & Chr(10) & " function [id]espopup_DragDrop(e)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " [id]mousemoveBack=document.body.onmousemove;" & Chr(13) & "" & Chr(10) & " [id]mouseupBack=document.body.onmouseup;" & Chr(13) & "" & Chr(10) & " ox=(e.offsetX==null)?e.layerX:e.offsetX;" & Chr(13) & "" & Chr(10) & " oy=(e.offsetY==null)?e.layerY:e.offsetY;" & Chr(13) & "" & Chr(10) & " [id]ofsX=ox; [id]ofsY=oy;" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " document.body.onmousemove=[id]espopup_DragDropMove;" & Chr(13) & "" & Chr(10) & " document.body.onmouseup=[id]espopup_DragDropStop;" & Chr(13) & "" & Chr(10) & " if ([id]tmrHide!=-1) clearInterval([id]tmrHide);" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function [id]espopup_DragDropMove(e)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el=document.getElementById('[id]'); " & Chr(13) & "" & Chr(10) & " if (e==null&&event!=null)" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el.style.left=(event.clientX*1+document.body.scrollLeft-[id]ofsX)+'px';" & Chr(13) & "" & Chr(10) & " el.style.top=(event.clientY*1+document.body.scrollTop-[id]ofsY)+'px';" & Chr(13) & "" & Chr(10) & " event.cancelBubble=true;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " else" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " el.style.left=(e.pageX*1-[id]ofsX)+'px';" & Chr(13) & "" & Chr(10) & " el.style.top=(e.pageY*1-[id]ofsY)+'px';" & Chr(13) & "" & Chr(10) & " e.cancelBubble=true;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " if ((event.button&1)==0) [id]espopup_DragDropStop();" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function [id]espopup_DragDropStop()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " document.body.onmousemove=[id]mousemoveBack;" & Chr(13) & "" & Chr(10) & " document.body.onmouseup=[id]mouseupBack;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " //]]>" & Chr(13) & "" & Chr(10) & " </script>"

        ''' <summary> 
        ''' Initialize script, styles and colors 
        ''' </summary> 
        Public Sub New()
            ' {0} = E5EDFA - popupBackground 
            ' {1} = 455690 - popupBorderDark 
            ' {2} = A6B4CF - popupBorderLight 
            ' {3} = 728EB8 - cntBorderDark 
            ' {4} = B9C9EF - cntBorderLight 
            ' {5} = E9EFF9 - cntBackground 
            ' {6} = E0E9F8 - gradientStart 
            ' {7} = FFFFFF - gradientEnd 
            ' {8} = 1F336B - textColor 
            ' {9} = 6A87B2 - xButton 
            ' {10}= 45638F - xButtonOver 

            divDesign = "background:#{0}; border-right:1px solid #{1}; border-bottom:1px solid #{1};" & Chr(13) & "" & Chr(10) & " border-left:1px solid #{2}; border-top:1px solid #{2}; position:absolute;" & Chr(13) & "" & Chr(10) & " z-index:9999; "

            cntStyle = "border-left:1px solid #{3}; border-top:1px solid #{3};" & Chr(13) & "" & Chr(10) & " border-bottom:1px solid #{4}; border-right:1px solid #{4};" & Chr(13) & "" & Chr(10) & " background:#{5}; padding:2px; overflow:hidden; text-align:center;" & Chr(13) & "" & Chr(10) & " filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0," & Chr(13) & "" & Chr(10) & " StartColorStr='#FF{6}', EndColorStr='#FF{7}');"
            cntStyleI = "position:absolute; left:2px; width:{0}px; top:20px; height:{1}px;"
            cntStyleN = "position:absolute; left:2px; width:{0}px; top:20px; height:{1}px;"

            aStyle = "font:12px arial,sans-serif; color:#{8}; text-decoration:none;"
            aCommands = "onmouseover=""style.textDecoration='underline';""" & Chr(13) & "" & Chr(10) & " onmouseout=""style.textDecoration='none';""" & Chr(13) & "" & Chr(10) & " href=""[cmd]"""

            hdrStyle = "position:absolute; left:2px; width:[wid]px; top:2px; height:14px;" & Chr(13) & "" & Chr(10) & " filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0," & Chr(13) & "" & Chr(10) & " StartColorStr='#FF{6}', EndColorStr='#FF{7}');"

            m_title = "Title here"
            msg = "Message to show in popup"
            fullmsg = "Text to display in new window."

            closeHtml = "<span style=""position:absolute; right:0px; top:0px; cursor:pointer; color:#{9}; font:bold 12px arial,sans-serif; " & Chr(13) & "" & Chr(10) & " position:absolute; right:3px;""" & Chr(13) & "" & Chr(10) & " onclick=""[id]espopup_Close()""" & Chr(13) & "" & Chr(10) & " onmousedown=""event.cancelBubble=true;""" & Chr(13) & "" & Chr(10) & " onmouseover=""style.color='#{10}';""" & Chr(13) & "" & Chr(10) & " onmouseout=""style.color='#{9}';"">X</span>"

            sPopup = "<head><title>{1}</title><style type=\""text/css\"">{2}</style></head>" + "<body><h1>{1}</h1><p>{0}</p></body>"

            spopStyle = "body {" + " background:#[gs]; padding:5px;" + " filter:progid:DXImageTransform.Microsoft.Gradient(" + " GradientType=0,StartColorStr='#FF[gs]', EndColorStr='#FF[ge]');" + " }" + " h1 {" + " font:bold 16px arial,sans-serif; color:#[clr]; " + " text-align:center; margin:0px;" + " }" + " p {" + " font:14px arial,sans-serif; color:#[clr];" + " }"

            ColorStyle = PopupColorStyle.Blue
            xOffset = yOffset = 15
            popDock = PopupDocking.BottomRight
            iHide = 5000
            winSize = New Size(400, 250)
            Width = New Unit("200px")
            Height = New Unit("100px")
            startTime = 1000
            popAction = PopupAction.MessageWindow
        End Sub

#End Region
#Region "Properties"


        ''' <summary> 
        ''' Get or set type of action 
        ''' </summary> 
        <Category("Action"), Description("Type of action (after link is clicked).")> _
        Public Property ActionType() As PopupAction
            Get
                Return popAction
            End Get
            Set(ByVal value As PopupAction)
                popAction = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set text to display in new window 
        ''' (when user clicked on popup window) 
        ''' </summary> 
        <Bindable(True), Category("Action"), Description("This text will be displayed in new window.")> _
        Public Property Text() As String
            Get
                Return fullmsg
            End Get
            Set(ByVal value As String)
                fullmsg = value.Replace("\", "\\").Replace("""", "\""")
            End Set
        End Property


        ''' <summary> 
        ''' Get or set adress/javascript to open when link is clicked 
        ''' </summary> 
        <Bindable(True), Category("Action"), Description("Adress/javascript to open when link is clicked.")> _
        Public Property Link() As String
            Get
                Return sLink
            End Get
            Set(ByVal value As String)
                sLink = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set target for specified link 
        ''' </summary> 
        <Bindable(True), Category("Action"), Description("Target for specified link.")> _
        Public Property LinkTarget() As String
            Get
                Return sTarget
            End Get
            Set(ByVal value As String)
                sTarget = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set predefined color style 
        ''' </summary> 
        <Category("Design"), Description("Predefined color style.")> _
        Public Property ColorStyle() As PopupColorStyle
            Get
                Return clrStyle
            End Get
            Set(ByVal value As PopupColorStyle)
                clrStyle = value
                Select Case clrStyle
                    Case PopupColorStyle.Blue
                        m_textColor = "1F336B"
                        xButtonOver = "455690"
                        popupBorderDark = "455690"
                        xButton = "728EB8"
                        cntBorderDark = "728EB8"
                        popupBorderLight = "B9C9EF"
                        cntBorderLight = "B9C9EF"
                        popupBackground = "E0E9F8"
                        cntBackground = "E0E9F8"
                        gradientStart = "E0E9F8"
                        gradientEnd = "FFFFFF"
                        Exit Select
                    Case PopupColorStyle.Violet

                        m_textColor = "200040"
                        xButtonOver = "400080"
                        popupBorderDark = "400080"
                        xButton = "7D5AA0"
                        cntBorderDark = "7D5AA0"
                        popupBorderLight = "B9AAC8"
                        cntBorderLight = "B9AAC8"
                        popupBackground = "D2C8DC"
                        cntBackground = "D2C8DC"
                        gradientStart = "D2C8DC"
                        gradientEnd = "FFFFFF"
                        Exit Select
                    Case PopupColorStyle.Green

                        m_textColor = "004000"
                        xButtonOver = "008000"
                        popupBorderDark = "008000"
                        xButton = "5AA05A"
                        cntBorderDark = "5AA05A"
                        popupBorderLight = "AAC8AA"
                        cntBorderLight = "AAC8AA"
                        popupBackground = "C8DCC8"
                        cntBackground = "C8DCC8"
                        gradientStart = "C8DCC8"
                        gradientEnd = "FFFFFF"
                        Exit Select
                    Case PopupColorStyle.Red

                        m_textColor = "400000"
                        xButtonOver = "800000"
                        popupBorderDark = "800000"
                        xButton = "A05A5A"
                        cntBorderDark = "A05A5A"
                        popupBorderLight = "C8AAAA"
                        cntBorderLight = "C8AAAA"
                        popupBackground = "DCC8C8"
                        cntBackground = "DCC8C8"
                        gradientStart = "DCC8C8"
                        gradientEnd = "FFFFFF"
                        Exit Select
                    Case Else


                        'blue 
                        m_textColor = "1F336B"
                        xButtonOver = "455690"
                        popupBorderDark = "455690"
                        xButton = "728EB8"
                        cntBorderDark = "728EB8"
                        popupBorderLight = "B9C9EF"
                        cntBorderLight = "B9C9EF"
                        popupBackground = "E0E9F8"
                        cntBackground = "E0E9F8"
                        gradientStart = "E0E9F8"
                        gradientEnd = "FFFFFF"
                        Exit Select
                End Select
            End Set
        End Property


        ''' <summary> 
        ''' Get or set message to display in popup window 
        ''' </summary> 
        <Bindable(True), Category("Texts"), Description("Message in popup.")> _
        Public Property Message() As String
            Get
                Return msg
            End Get
            Set(ByVal value As String)
                msg = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set title to display in popup window and new window 
        ''' </summary> 
        <Bindable(True), Category("Texts"), Description("Title of popup element and new window.")> _
        Public Property Title() As String
            Get
                Return m_title
            End Get
            Set(ByVal value As String)
                m_title = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set ligh Gradient color 
        ''' </summary> 
        <Bindable(True), Category("Design"), Description("Ligh Gradient color.")> _
        Public Property GradientLight() As Color
            Get
                Return ColorFromString(gradientEnd)
            End Get
            Set(ByVal value As Color)
                ColorStyle = PopupColorStyle.[Custom]
                gradientEnd = ColorToString(value)
            End Set
        End Property


        ''' <summary> 
        ''' Get or set dark gradient color (Background in Mozilla) 
        ''' </summary> 
        <Bindable(True), Category("Design"), Description("Dark gradient color (Background in Mozilla).")> _
        Public Property GradientDark() As Color
            Get
                Return ColorFromString(gradientStart)
            End Get
            Set(ByVal value As Color)
                ColorStyle = PopupColorStyle.[Custom]
                popupBackground = cntBackground = gradientStart = ColorToString(value)
            End Set
        End Property


        ''' <summary> 
        ''' Get or set text color 
        ''' </summary> 
        <Bindable(True), Category("Design"), Description("Text color.")> _
        Public Property TextColor() As Color
            Get
                Return ColorFromString(m_textColor)
            End Get
            Set(ByVal value As Color)
                ColorStyle = PopupColorStyle.[Custom]
                m_textColor = ColorToString(value)
            End Set
        End Property


        ''' <summary> 
        ''' Get or set light shadow color 
        ''' </summary> 
        <Bindable(True), Category("Design"), Description("Light shadow color.")> _
        Public Property LightShadow() As Color
            Get
                Return ColorFromString(popupBorderLight)
            End Get
            Set(ByVal value As Color)
                ColorStyle = PopupColorStyle.[Custom]
                popupBorderLight = cntBorderLight = ColorToString(value)
            End Set
        End Property


        ''' <summary> 
        ''' Get or set dark shadow color 
        ''' </summary> 
        <Bindable(True), Category("Design"), Description("Dark shadow color.")> _
        Public Property DarkShadow() As Color
            Get
                Return ColorFromString(xButtonOver)
            End Get
            Set(ByVal value As Color)
                ColorStyle = PopupColorStyle.[Custom]
                xButtonOver = popupBorderDark = ColorToString(value)
            End Set
        End Property


        ''' <summary> 
        ''' Get or set shadow color 
        ''' </summary> 
        <Bindable(True), Category("Design"), Description("Shadow color.")> _
        Public Property Shadow() As Color
            Get
                Return ColorFromString(xButton)
            End Get
            Set(ByVal value As Color)
                ColorStyle = PopupColorStyle.[Custom]
                xButton = cntBorderDark = ColorToString(value)
            End Set
        End Property


        ''' <summary> 
        ''' Get or set popup window docking 
        ''' </summary> 
        <Category("Layout"), Description("Popup window docking.")> _
        Public Property DockMode() As PopupDocking
            Get
                Return popDock
            End Get
            Set(ByVal value As PopupDocking)
                popDock = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set x offset 
        ''' </summary> 
        <Category("Layout"), Description("X offset (from left or right)."), DefaultValue(15)> _
        Public Property OffsetX() As Integer
            Get
                Return xOffset
            End Get
            Set(ByVal value As Integer)
                xOffset = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set y offset 
        ''' </summary> 
        <Category("Layout"), Description("Y offset from bottom."), DefaultValue(15)> _
        Public Property OffsetY() As Integer
            Get
                Return yOffset
            End Get
            Set(ByVal value As Integer)
                yOffset = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set how long will window be displayed 
        ''' </summary> 
        <Bindable(True), Category("Behavior"), DefaultValue(500), Description("How long will be window displayed in milliseconds(-1 for infinite).")> _
        Public Property HideAfter() As Integer
            Get
                Return iHide
            End Get
            Set(ByVal value As Integer)
                iHide = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set how fast will showing of popup be 
        ''' </summary> 
        <Bindable(True), Category("Behavior"), DefaultValue(20), Description("Get or set how fast will showing of popup be.")> _
        Public Property PopupSpeed() As Integer
            Get
                Return iSpeed
            End Get
            Set(ByVal value As Integer)
                iSpeed = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set delay before displaying popup control 
        ''' </summary> 
        <Bindable(True), Category("Behavior"), DefaultValue(1000), Description("Delay before displaying popup control.")> _
        Public Property ShowAfter() As Integer
            Get
                Return startTime
            End Get
            Set(ByVal value As Integer)
                startTime = value
            End Set
        End Property


        ''' <summary> 
        ''' Automaticly show popup when page loads (after ShowAfter miliseconds). 
        ''' </summary> 
        <Category("Behavior"), DefaultValue(True)> _
        <Description("Automaticly show popup when page loads (after ShowAfter miliseconds).")> _
        Public Property AutoShow() As Boolean
            Get
                Return bAutoShow
            End Get
            Set(ByVal value As Boolean)
                bAutoShow = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set wether user can move popup element 
        ''' </summary> 
        <Category("Behavior"), DefaultValue(True)> _
        <Description("Allow user to move popup element.")> _
        Public Property DragDrop() As Boolean
            Get
                Return bDragDrop
            End Get
            Set(ByVal value As Boolean)
                bDragDrop = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set window size 
        ''' </summary> 
        <Bindable(True), Category("Window"), Description("Opened window size.")> _
        Public Property WindowSize() As Size
            Get
                Return winSize
            End Get
            Set(ByVal value As Size)
                winSize = value
            End Set
        End Property


        ''' <summary> 
        ''' Get or set window scrollbars 
        ''' </summary> 
        <Bindable(True), Category("Window"), Description("Display scrollbars in new window."), DefaultValue(True)> _
        Public Property WindowScroll() As Boolean
            Get
                Return winScroll
            End Get
            Set(ByVal value As Boolean)
                winScroll = value
            End Set
        End Property


        ''' <summary> 
        ''' Generate link inside popup ? 
        ''' </summary> 
        <Bindable(True), DefaultValue(True), Category("Action"), Description("Generate link inside popup and enable action ?")> _
        Public Property ShowLink() As Boolean
            Get
                Return bShowLink
            End Get
            Set(ByVal value As Boolean)
                bShowLink = value
            End Set
        End Property


#End Region
#Region "Methods"

        ''' <summary> 
        ''' Convert color to string (no color names !) 
        ''' </summary> 
        ''' <param name="color">Color</param> 
        ''' <returns>String (without # prefix)</returns> 
        Private Function ColorToString(ByVal color As Color) As String
            Return color.R.ToString("x").PadLeft(2, "0"c) + color.G.ToString("x").PadLeft(2, "0"c) + color.B.ToString("x").PadLeft(2, "0"c)
        End Function


        ''' <summary> 
        ''' Convert string in RRGGBB format to Gdi+ color 
        ''' </summary> 
        ''' <param name="str">String to convert</param> 
        ''' <returns>Gdi+ color</returns> 
        Private Function ColorFromString(ByVal str As String) As Color
            Return ColorTranslator.FromHtml("#" + str)
        End Function


        ''' <summary> 
        ''' Replace {0}..{10} with colors 
        ''' </summary> 
        ''' <param name="html">Html source</param> 
        Private Function PutColors(ByVal html As String) As String
            Return [String].Format(html, popupBackground, popupBorderDark, popupBorderLight, cntBorderDark, cntBorderLight, _
            cntBackground, gradientStart, gradientEnd, m_textColor, xButton, xButtonOver)
        End Function

        ''' <summary> 
        ''' Render this control to the output parameter specified. 
        ''' </summary> 
        ''' <param name="output"> The HTML writer to write out to </param> 
        Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)
            Dim br As String = Page.Request.Browser.Browser
            Dim script As String = sScript, sps As String = spopStyle

            Dim acmd As String = ""
            Select Case popAction
                Case PopupAction.MessageWindow
                    acmd = aCommands.Replace("[cmd]", "javascript:[id]espopup_ShowWindow();")
                    script = script.Replace("[slink]", "")
                    script = script.Replace("[sclose]", "")
                    Exit Select
                Case PopupAction.RaiseEvents
                    acmd = aCommands.Replace("[cmd]", "javascript:[id]espopup_ShowWindow();")
                    Dim scriptClick As String = Page.ClientScript.GetPostBackEventReference(Me, "C") + "; return;"
                    Dim scriptClose As String = Page.ClientScript.GetPostBackEventReference(Me, "X") + "; return;"
                    script = script.Replace("[slink]", scriptClick)
                    script = script.Replace("[sclose]", scriptClose)
                    Exit Select
                Case PopupAction.OpenLink
                    acmd = aCommands.Replace("[cmd]", sLink)
                    script = script.Replace("[slink]", "")
                    script = script.Replace("[sclose]", "")
                    If sTarget <> "" Then
                        acmd += " target=""" + sTarget + """"
                    End If
                    Exit Select
            End Select

            sps = sps.Replace("[gs]", gradientStart)
            sps = sps.Replace("[ge]", gradientEnd)
            sps = sps.Replace("[clr]", m_textColor)

            script = script.Replace("[winstyle]", [String].Format("width={0},height={1},scrollbars={2}", winSize.Width, winSize.Height, (IIf(winScroll, "yes", "no"))))
            script = script.Replace("[hide]", iHide.ToString())
            script = script.Replace("[stime]", startTime.ToString())
            script = script.Replace("[id]", ClientID)
            script = script.Replace("[ie]", (IIf(br = "IE", "true", "false")))
            script = script.Replace("[popup]", [String].Format(sPopup, fullmsg, m_title, sps))
            script = script.Replace("[autoshow]", bAutoShow.ToString().ToLower())
            script = script.Replace("[speed]", iSpeed.ToString())

            Dim divPos As String = [String].Format("width:{0}; height:{1}; ", Width, Height)
            Dim cntsI As String = [String].Format(cntStyleI, Width.Value - 6, Height.Value - 24)
            Dim cntsN As String = [String].Format(cntStyleN, Width.Value - 10, Height.Value - 28)

            Dim sDragDrop As String = ""
            If bDragDrop Then
                sDragDrop = " onmousedown=""return " + ClientID + "espopup_DragDrop(event);"" "
            End If
            If popDock = PopupDocking.BottomLeft Then
                divPos += "left:"
            Else
                divPos += "right:"
            End If
            divPos += String.Format("{0}px; bottom:{1}px;", xOffset, yOffset)
            output.Write(script + [String].Format("<div id=""{0}"" " + "style=""display:none; {1} {2}"" onselectstart=""return false;"" {4}>" + "<div id=""{3}"" style=""cursor:default; display:none; {5}"">{6}</div>" + "<div id=""{7}"" onmousedown=""event.cancelBubble=true;"" style=""display:none; {8}"">" + (IIf((bShowLink = True), "<a style=""{9}"" {10} id=""{11}"">{12}</a></div></div>", "<span style=""{9}"" id=""{11}"">{12}</span></div></div>")), ClientID, PutColors(divDesign), divPos, ClientID + "_header", sDragDrop, _
            PutColors(hdrStyle).Replace("[wid]", (Width.Value - 6).ToString()) + PutColors(aStyle), "<span id=""" + ClientID + "titleEl"">" + m_title + "</span>" + PutColors(closeHtml).Replace("[id]", ClientID), ClientID + "_content", PutColors(cntStyle) + (IIf((br <> "Netscape" AndAlso br <> "Mozilla"), cntsI, cntsN)), PutColors(aStyle), acmd.Replace("[id]", ClientID), _
            ClientID + "aCnt", msg))
        End Sub


        ''' <summary> 
        ''' Generate html code to show in new window 
        ''' </summary> 
        ''' <param name="title">Title</param> 
        ''' <param name="text">Text</param> 
        ''' <returns>Html code for new window</returns> 
        Friend Function GetWinText(ByVal title As String, ByVal text As String) As String
            Dim sps As String = spopStyle
            sps = sps.Replace("[gs]", gradientStart)
            sps = sps.Replace("[ge]", gradientEnd)
            sps = sps.Replace("[clr]", m_textColor)

            Return [String].Format(sPopup, text.Replace("\", "\\").Replace("""", "\"""), title, sps)
        End Function

        ''' <summary> 
        ''' Returns html code for designer 
        ''' </summary> 
        Friend Function GetDesignCode() As String
            Dim divPos As String = [String].Format("width:{0}; height:{1}; ", Width, Height)
            Dim cntsI As String = [String].Format(cntStyleI, Width.Value - 6, Height.Value - 24)

            Return [String].Format("<div id=""{0}"" " + "style=""{1} {2}; left:0px; top:0px; "">" + "<div id=""{3}"" style=""{4}"">{5}</div>" + "<div id=""{6}"" style=""{7}"">" + "<a style=""{8}"" {9}>{10}</a></div></div>", ClientID, PutColors(divDesign), divPos, ClientID + "_header", PutColors(hdrStyle).Replace("[wid]", (Width.Value - 6).ToString()) + PutColors(aStyle), _
            m_title + PutColors(closeHtml).Replace("[id]", ClientID), ClientID + "_content", PutColors(cntStyle) + cntsI, PutColors(aStyle), aCommands.Replace("[id]", ClientID), msg)
        End Function

#End Region
#Region "Events and Event handlers"

        ''' <summary> 
        ''' Raise event 
        ''' </summary> 
        ''' <param name="eventArgument">Closed or clicked on link ?</param> 
        Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
            If eventArgument = "C" Then
                RaiseEvent LinkClicked(Me, EventArgs.Empty)
            End If
            If eventArgument = "X" Then
                RaiseEvent PopupClosed(Me, EventArgs.Empty)
            End If
        End Sub


        ''' <summary> 
        ''' User clicked on link on popup box 
        ''' </summary> 
        <Category("Popup"), Description("User clicked on link on popup box.")> _
        Public Event LinkClicked As EventHandler


        ''' <summary> 
        ''' User clicked on 'X' on popup box. 
        ''' </summary> 
        <Category("Popup"), Description("User clicked on 'X' on popup box.")> _
        Public Event PopupClosed As EventHandler

#End Region
    End Class

    ''' <summary> 
    ''' Class for displaying PopupWin in designer 
    ''' </summary> 
    Public Class PopupWinDesigner
        Inherits ControlDesigner
#Region "Overriden methods"

        ''' <summary> 
        ''' Returns HTML code to show in designer 
        ''' </summary> 
        Public Overloads Overrides Function GetDesignTimeHtml() As String
            Try
                Return DirectCast(Component, PopupWin).GetDesignCode()
            Catch er As Exception
                Return GetErrorDesignTimeHtml(er)
            End Try
        End Function

#End Region
    End Class


    ''' <summary> 
    ''' Predefined color style 
    ''' </summary> 
    Public Enum PopupColorStyle
#Region "Members"

        ''' <summary> Blue style (default) </summary> 
        Blue
        ''' <summary> Red style </summary> 
        Red
        ''' <summary> Green style </summary> 
        Green
        ''' <summary> Violet style </summary> 
        Violet
        ''' <summary> Custom style - defined by colors </summary> 
        [Custom]

#End Region
    End Enum


    ''' <summary> 
    ''' Popup window docking 
    ''' </summary> 
    Public Enum PopupDocking
#Region "Members"

        ''' <summary> Control is docked to left and bottom </summary> 
        BottomLeft
        ''' <summary> Control is docked to right and bottom </summary> 
        BottomRight

#End Region
    End Enum


    ''' <summary> 
    ''' Action to do after user clicked on link 
    ''' </summary> 
    Public Enum PopupAction
#Region "Members"

        ''' <summary> Raise server events </summary> 
        RaiseEvents
        ''' <summary> Open new browser window with text message </summary> 
        MessageWindow
        ''' <summary> Open link or javascript script </summary> 
        OpenLink

#End Region
    End Enum
End Namespace